<?php


namespace App\Libs\Contracts\PayMent\AliPay\App\aop;




use App\Libs\Contracts\PayMent\AliPay\App\aop\request\AlipayTradeAppPayRequest;
use App\Libs\Contracts\PayMent\AliPay\Web\pagepay\buildermodel\AlipayTradeRefundContentBuilder;
use App\Libs\Contracts\PayMent\AliPay\Web\pagepay\service\AlipayTradeService;

class AliApp
{
    public function pay($order,$appType = '')
    {
        $orderNo = $order['orderNo'];
        $body = $order['body'];
        $title = $order['title'];
        $totalPrice =  $order['totalPrice'];
        $totalPrice = $totalPrice / 100;
        $aop = new AopClient();
        $aop->gatewayUrl = env("ALI_GATEWAYURL");
        if($appType != "TEACHER"){
            $aop->appId = env("ALI_APPID");
            $aop->rsaPrivateKey = env("ALI_MERCHANT_PRIVATE_KEY");
            $aop->format = "json";
            $aop->charset =env("ALI_CHARSET");
            $aop->signType = env("ALI_SIGN_TYPE");
            $aop->alipayrsaPublicKey = env("ALI_PAY_PUBLIC_KEY");
        }else{
            $aop->appId = env("ALI_TEACHER_APPID");
            $aop->rsaPrivateKey = env("ALI_TEACHER_MERCHANT_PRIVATE_KEY");
            $aop->format = "json";
            $aop->charset =env("ALI_CHARSET");
            $aop->signType = env("ALI_SIGN_TYPE");
            $aop->alipayrsaPublicKey = env("ALI_TEACHER_PAY_PUBLIC_KEY");
        }

//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        $request = new AlipayTradeAppPayRequest();
//SDK已经封装掉了公共参数，这里只需要传入业务参数
        $bizcontent = "{\"body\":\"$body\","
            . "\"subject\": \"$title\","
            . "\"out_trade_no\": \"$orderNo\","
            . "\"timeout_express\": \"30m\","
            . "\"total_amount\": \"$totalPrice\""
            . "}";
        if ($order['type']){
            $request->setNotifyUrl(env("ALI_RECHARGE_NOTIFY_URL"));

        }else{
            $request->setNotifyUrl(env("ALI_NOTIFY_URL"));

        }
        $request->setBizContent($bizcontent);
//这里和普通的接口调用不同，使用的是sdkExecute
        $response = $aop->sdkExecute($request);
        return $response;
//htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
        //echo htmlspecialchars($response);//就是orderString 可以直接给客户端请求，无需再做处理。
    }



    public function refund($order)
    {
        $config = getAliConfig();
        $out_trade_no = trim($order['orderNo']);

        //需要退款的金额，该金额不能大于订单金额，必填
        $refund_amount = trim($order['refundPrice']);

        //退款的原因说明
        $refund_reason = trim($order['refundReason']);

        //标识一次退款请求，同一笔交易多次退款需要保证唯一，如需部分退款，则此参数必传
        $out_request_no = trim($order['refundNo']);

        //构造参数
        $RequestBuilder=new AlipayTradeRefundContentBuilder();
        $RequestBuilder->setOutTradeNo($out_trade_no);
        $RequestBuilder->setRefundAmount($refund_amount);
        $RequestBuilder->setOutRequestNo($out_request_no);
        $RequestBuilder->setRefundReason($refund_reason);

        $aop = new AlipayTradeService($config);

        /**
         * alipay.trade.refund (统一收单交易退款接口)
         * @param $builder 业务参数，使用buildmodel中的对象生成。
         * @return $response 支付宝返回的信息
         */
        $response = $aop->Refund($RequestBuilder);
        $resultCode = $response->code;

        if(!empty($resultCode)  &&  $resultCode == 10000){
            return true;
        } else {
            return false;
        }
    }
}